package gui;

import information.Job;
import information.Park;
import information.User;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import actions.ApplyAction;
import actions.BlackballAction;
import actions.SortJobsAction;
import actions.SubmitJobAction;
import actions.ViewJobsAction;

/**
 * A display the user will see once they log in to the system. 
 * The display contains user specific actions. 
 * 
 * @author Natalie
 * @author Christian
 *
 */
@SuppressWarnings("serial")
public class UserPanel extends JPanel {
	
	public static final int PANEL_HEIGHT = 600;
	
	public static final int PANEL_WIDTH = 700;
	/**
	 * The users available actions. 
	 */
	private List<AbstractAction> myActions;
	
	/**
	 * Creates a user panel based on the users available actions.
	 * 
	 * @param theActions The current users available actions. 
	 * @author Natalie
	 */
	public UserPanel(List<AbstractAction> theActions) {
		
		myActions = theActions;
	}
	
	/**
	 * Creates a home screen containing the current users available actions.
	 * @author Natalie 
	 */
	public void createHome() {
		removeAll();
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		for (AbstractAction action : myActions) {
			add(new JLabel("  "));
			add(new JButton(action));
		}
		repaint();
	}
	
	/**
	 * Displays a job form for a manager to enter a new job's information. 
	 * 
	 * @param theFrame The frame the job form will be displayed on. 
	 * @author Natalie
	 */
	public void displayJobForm(final Frame theFrame) {
		removeAll();
		setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		JLabel title = new JLabel("Enter the New Job Information");
		title.setFont(new Font("Tahoma", Font.BOLD, 13));
		add(title); 
				
		add(new JLabel("Park Manager : " + theFrame.getData().getCurrentUser().getName()));
		
		add(new JLabel("Park : "));
		Vector<String> parkNames = new Vector<String>();
		for (Park parkname : theFrame.getData().getCurrentUser().getParks()) {
			parkNames.add(parkname.getMyName());
		}
		JComboBox parkList = new JComboBox(parkNames);
		add(parkList);
				
		add(new JLabel("Job Description : "));
		JTextField jobDescription = new JTextField();
		add(jobDescription);
		
		add(new JLabel("Start Date : (Format: MM  DD  YYYY  Hour  Min)"));
		JTextField startDate = new JTextField();
		add(startDate);
		
		add(new JLabel("End Date : (Format: MM  DD  YYYY  Hour  Min)"));
		JTextField endDate = new JTextField();
		add(endDate);
		
		JCheckBox weekday = new JCheckBox("Spans Weekday? M-F");
		add(weekday);
		JCheckBox holiday = new JCheckBox("Spans Holiday?");
		add(holiday);
		
		add(new JLabel("Number of Light Volunteers : "));
		JTextField lightVols = new JTextField();
		add(lightVols);
		add(new JLabel("Light Work Description : "));
		JTextField lightDescription = new JTextField();
		add(lightDescription);
		
		add(new JLabel("Number of Medium Volunteers : "));
		JTextField mediumVols = new JTextField();
		add(mediumVols);
		add(new JLabel("Medium Work Description : "));
		JTextField mediumDescription = new JTextField();
		add(mediumDescription);
		
		add(new JLabel("Number of Heavy Volunteers : "));
		JTextField heavyVols = new JTextField();
		add(heavyVols);
		add(new JLabel("Heavy Work Description : "));
		JTextField heavyDescription = new JTextField();
		add(heavyDescription);
		
		add(new JButton(new SubmitJobAction(theFrame, parkList, jobDescription, startDate, endDate, weekday, holiday, 
				lightVols, lightDescription, mediumVols, mediumDescription, heavyVols, heavyDescription)));
		
		repaint();
	}
	
	/**
	 * Displays a message to the user letting them know their task was completed successfully. 
	 * @author Natalie
	 */
	public void displaySuccess(final String theMessage) {
		removeAll();
		add(new JLabel(theMessage));
		repaint();
	}
	
	/**
	 * Displays a list sortable by district, region, park, and date
	 * of jobs to browse. List is limited to jobs that have openings, and don't overlap jobs
	 * the volunteer has signed up for.
	 * User story 7. BR 3,6,7.
	 * @author Christian
	 * @param theJobs
	 * @param theSortCriteria
	 * @param theFrame
	 */
	public void browseJobs(final List<Job> theJobs, final List<String> theSortCriteria, final Frame theFrame) {
		removeAll();
		setLayout(new BorderLayout());
		int columns = 7;
		JPanel ctrPanel = new JPanel();
		JPanel topPanel = new JPanel();
		
		GridLayout gLayout = new GridLayout(0,columns);
		gLayout.setHgap(5);
		ctrPanel.setLayout(gLayout);
		
		//create header display
		JLabel titleLabel = new JLabel("Upcoming Jobs");
		JButton regionLabel = new JButton(new SortJobsAction(theFrame, "Region", theSortCriteria));
		regionLabel.setVerticalAlignment(SwingConstants.TOP);
		regionLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		JButton districtLabel = new JButton(new SortJobsAction(theFrame, "District", theSortCriteria));
		districtLabel.setVerticalAlignment(SwingConstants.TOP);
		districtLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		JButton locationLabel = new JButton(new SortJobsAction(theFrame, "Park", theSortCriteria));
		locationLabel.setVerticalAlignment(SwingConstants.TOP);
		locationLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		JButton datesLabel = new JButton(new SortJobsAction(theFrame, "Dates", theSortCriteria));
		datesLabel.setVerticalAlignment(SwingConstants.TOP);
		datesLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		JLabel levelLabel = new JLabel("Level : Positions Available");
		levelLabel.setVerticalAlignment(SwingConstants.TOP);
		levelLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		JLabel bbLabel = new JLabel("Apply?");
		bbLabel.setVerticalAlignment(SwingConstants.TOP);
		bbLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		for (int i = 0; i < columns; i++) { //spacing between title and guts
			ctrPanel.add(new JLabel());
		}
		//adding header data to panel
		ctrPanel.add(regionLabel);
		ctrPanel.add(districtLabel);
		ctrPanel.add(locationLabel);
		ctrPanel.add(datesLabel);
		ctrPanel.add(new JLabel());
		ctrPanel.add(levelLabel);
		ctrPanel.add(bbLabel);
		ctrPanel.add(new JSeparator());
		ctrPanel.add(new JSeparator());
		ctrPanel.add(new JSeparator());
		ctrPanel.add(new JSeparator());
		ctrPanel.add(new JLabel());
		ctrPanel.add(new JSeparator());
		ctrPanel.add(new JSeparator());
		titleLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		//display guts
		for (Job job : theJobs) {
			Park park = job.getPark();
			JLabel region = new JLabel(park.getMyRegion());
			JLabel district = new JLabel(park.getMyDistrict());
			JLabel location = new JLabel(park.getMyLocation());
			String fromDate = job.getStartDate().toString();
			String[] splitDate = fromDate.split(" ");
			fromDate = splitDate[0];
			String toDate = job.getEndDate().toString();
			splitDate = toDate.split(" ");
			toDate = splitDate[0];
			JLabel dates = new JLabel(fromDate + " - " + toDate);
			//adding level dropdown catting positions available to dropdown
			//only if positions available for that level
			//Business rule 3
			Vector<String> levels = new Vector<String>();
			int lightAvailable = job.getLightNumber() - job.getPositionsFilled(0);
			if (lightAvailable > 0) {
				levels.add("Light: " + lightAvailable);
			}
			int medAvailable = job.getMediumNumber() - job.getPositionsFilled(1);
			if (medAvailable > 0) {
				levels.add("Medium: " + medAvailable);
			}
			int heavyAvailable = job.getHeavyNumber() - job.getPositionsFilled(2);
			if (heavyAvailable > 0) {
				levels.add("Heavy: " + heavyAvailable);
			}
			JComboBox level = new JComboBox(levels);
			
			//add guts to panel
			ctrPanel.add(region);
			ctrPanel.add(district);
			ctrPanel.add(location);
			ctrPanel.add(dates);
			ctrPanel.add(new JButton(new ViewJobsAction(job, "View Job Info", theFrame.getData().getCurrentUser())));
			ctrPanel.add(level);
			ctrPanel.add(new JButton(new ApplyAction(theFrame, job, level)));
			ctrPanel.add(new JLabel());
			ctrPanel.add(new JLabel());
			ctrPanel.add(new JLabel());
			ctrPanel.add(new JLabel());
			ctrPanel.add(new JLabel());
			ctrPanel.add(new JLabel());
			ctrPanel.add(new JLabel());
		}
		topPanel.add(titleLabel);		
		add(ctrPanel, BorderLayout.CENTER);
		add(topPanel, BorderLayout.NORTH);
		repaint();
	}
	
	/**
	 * User Story 8 (As a Staff Member I want to be able to blackball a Volunteer).
	 * Displays a list of volunteers to be blackballed.
	 * This list omits volunteers that are already blackballed, including the "just-blackballed" 
	 * volunteer on page refresh.
	 * @author Christian
	 * @param theFrame
	 * @param volunteerList
	 */
	public void displayVolunteers(final Frame theFrame, final Set<User> volunteerList) {
		removeAll();
		setLayout(new BorderLayout());
		int columns = 4;
		JPanel ctrPanel = new JPanel();
		JPanel topPanel = new JPanel();
		
		GridLayout gLayout = new GridLayout(0,columns);
		gLayout.setHgap(20);
		ctrPanel.setLayout(gLayout);
		
		//create header data
		JLabel titleLabel = new JLabel("Volunteers");
		JLabel nameLabel = new JLabel("Name");
		nameLabel.setVerticalAlignment(SwingConstants.TOP);
		nameLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		JLabel emailLabel = new JLabel("Email");
		emailLabel.setVerticalAlignment(SwingConstants.TOP);
		emailLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		JLabel phoneLabel = new JLabel("Phone Number");
		phoneLabel.setVerticalAlignment(SwingConstants.TOP);
		phoneLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		JLabel bbLabel = new JLabel("Blackball?");
		bbLabel.setVerticalAlignment(SwingConstants.TOP);
		bbLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		for (int i = 0; i < columns; i++) { //spacing between title and guts
			ctrPanel.add(new JLabel());
		}
		//add header data to panel
		ctrPanel.add(nameLabel);
		ctrPanel.add(emailLabel);
		ctrPanel.add(phoneLabel);
		ctrPanel.add(bbLabel);
		ctrPanel.add(new JSeparator());
		ctrPanel.add(new JSeparator());
		ctrPanel.add(new JSeparator());
		ctrPanel.add(new JSeparator());
		titleLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		//create guts
		for (User volunteer : volunteerList) {
			JLabel name = new JLabel(volunteer.getName());
			JLabel email = new JLabel(volunteer.getEmail());
			JLabel phone = new JLabel(volunteer.getPhone());
			//add guts (for this volunteer) to panel
			ctrPanel.add(name);
			ctrPanel.add(email);
			ctrPanel.add(phone);
			ctrPanel.add(new JButton(new BlackballAction(theFrame, volunteer)));
		}
		topPanel.add(titleLabel);		
		add(ctrPanel, BorderLayout.CENTER);
		add(topPanel, BorderLayout.NORTH);
		repaint();
	}
	
	/**
	 * Displays a list of volunteers in the user's panel.
	 * @author Christian
	 * @param volunteerList The list of volunteers to be displayed. 
	 */
	public void displayVolunteerList(Set<User> volunteerList, String theTitle) {
		removeAll();
		int columns = 3;
		setLayout(new BorderLayout());
		
		JPanel ctrPanel = new JPanel();
		JPanel topPanel = new JPanel();
		
		GridLayout gLayout = new GridLayout(0,columns);
		gLayout.setHgap(20);
		gLayout.setVgap(10);
		ctrPanel.setLayout(gLayout);
		
		//create header data
		JLabel titleLabel = new JLabel(theTitle);
		JLabel nameLabel= new JLabel("Name");
		nameLabel.setVerticalAlignment(SwingConstants.TOP);
		nameLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		JLabel emailLabel= new JLabel("Email");
		emailLabel.setVerticalAlignment(SwingConstants.TOP);
		emailLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		JLabel phoneLabel= new JLabel("Phone Number");
		phoneLabel.setVerticalAlignment(SwingConstants.TOP);
		phoneLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		for (int i = 0; i < columns; i++) { //spacing between title and guts
			ctrPanel.add(new JLabel());
		}
		//add header data to panel
		ctrPanel.add(nameLabel);
		ctrPanel.add(emailLabel);
		ctrPanel.add(phoneLabel);
		ctrPanel.add(new JSeparator());
		ctrPanel.add(new JSeparator());
		ctrPanel.add(new JSeparator());
		titleLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		//create guts
		for (User volunteer : volunteerList) {
			JLabel name = new JLabel(volunteer.getName());
			JLabel email = new JLabel(volunteer.getEmail());
			JLabel phone = new JLabel(volunteer.getPhone());
			ctrPanel.add(name);
			ctrPanel.add(email);
			ctrPanel.add(phone);
		}
		topPanel.add(titleLabel);		
		add(ctrPanel, BorderLayout.CENTER);
		add(topPanel, BorderLayout.NORTH);
		repaint();
	}
	
	/**
	 * Displays all of the information for each of the jobs in the set passed in. 
	 * 
	 * @param theJobs The jobs to be displayed. 
	 * @author Natalie
	 */
	public void displayJobs(Set<Job> theJobs) {
		removeAll();
		int numberOfRows = theJobs.size();
		GridLayout gLayout = new GridLayout((numberOfRows * 18) + 1, 2);
		gLayout.setHgap(10);
		gLayout.setVgap(5);
		setLayout(gLayout);
		JLabel title = new JLabel("Job Information", JLabel.RIGHT);
		title.setFont(new Font("Tahoma", Font.BOLD, 13));
		add(title);
		add(new JLabel());
		for (Job ajob : theJobs) {
			add(new JSeparator());
			add(new JSeparator());
			add(new JLabel("  "));
			add(new JLabel("  "));
			add(new JLabel("Park : ", JLabel.RIGHT));
			add(new JLabel(ajob.getPark().getMyName()));
			add(new JLabel("Job Description : ", JLabel.RIGHT));
			add(new JLabel(ajob.getDescription()));
			add(new JLabel("Start Date : ", JLabel.RIGHT));
			add(new JLabel(ajob.getStartDate().toString()));
			add(new JLabel("End Date : ", JLabel.RIGHT));
			add(new JLabel(ajob.getEndDate().toString()));
			add(new JLabel("Is Weekday? ", JLabel.RIGHT));
			add(new JLabel("" + ajob.isWeekday()));
			add(new JLabel("Is Holiday? ", JLabel.RIGHT));
			add(new JLabel("" + ajob.isHoliday()));
			add(new JLabel("Number of Light Volunteers Needed : ", JLabel.RIGHT));
			add(new JLabel(ajob.getLightNumber().toString()));
			add(new JLabel("Number of Light Volunteers Signed Up : ", JLabel.RIGHT));
			add(new JLabel(ajob.getPositionsFilled(0).toString()));
			add(new JLabel("Light Work Description : ", JLabel.RIGHT));
			add(new JLabel(ajob.getLightDetials()));
			add(new JLabel("Number of Medium Volunteers Needed : ", JLabel.RIGHT));
			add(new JLabel(ajob.getMediumNumber().toString()));
			add(new JLabel("Number of Medium Volunteers Signed Up : ", JLabel.RIGHT));
			add(new JLabel(ajob.getPositionsFilled(1).toString()));
			add(new JLabel("Medium Work Description : ", JLabel.RIGHT));
			add(new JLabel(ajob.getMediumDetials()));
			add(new JLabel("Number of Heavy Volunteers Needed : ", JLabel.RIGHT));
			add(new JLabel(ajob.getHeavyNumber().toString()));
			add(new JLabel("Number of Heavy Volunteers Signed Up : ", JLabel.RIGHT));
			add(new JLabel(ajob.getPositionsFilled(2).toString()));
			add(new JLabel("Heavy Work Description : ", JLabel.RIGHT));
			add(new JLabel(ajob.getHeavyDetails()));
			add(new JLabel("  "));
			add(new JLabel("  "));
			
		}
		repaint();
	}
	
}
